<?php
include_once 'BaseS.php';
include_once $_SERVER ['DOCUMENT_ROOT'].BASE_PROJECT_PATH.'/application/model/MemberM.php';
include_once $_SERVER ['DOCUMENT_ROOT'].BASE_PROJECT_PATH.'/library/util/Util.php';
include_once $_SERVER ['DOCUMENT_ROOT'].BASE_PROJECT_PATH.'/library/util/Regex.php';
include_once $_SERVER ['DOCUMENT_ROOT'].BASE_PROJECT_PATH.'/library/util/Push.php';

class AppOwnerS extends BaseS {
	
	public function __construct() {
		parent::__construct ();
		parent::setTarget ( $this );
	}

	public function regist() {
		
		$result 		= array("status" => 0, "msg" => "", "mId" => 0, "token" => "" );
		$phoneType 		= $_POST['phoneType'];
		$phoneVersion 	= $_POST['phoneVersion'];
		$registType 	= $_POST['registType'] ;
		$account 		= $_POST['account'];
		$password 		= isset($_POST['password']) ? $_POST['password'] : "" ;
		$pushStatus		= isset($_POST['pushStatus']) ? $_POST['pushStatus'] : 1 ;
		$pushNo 		= $_POST['pushNo'];
		$deviceNo		= $_POST['deviceNo'];

		$dao = new MemberM();
		$push = new Push();
		
		if(!Regex::isNumber($phoneType, 1))		{$result["status"] = -3; Util::toEchoJson($result); exit;}
		if(!Regex::isFloat($phoneVersion, 30))	{$result["status"] = -3; Util::toEchoJson($result); exit;}
		
		if($phoneType == 1 && $phoneVersion <= ANDROID_1 ){
			
			switch($registType){
				
			//一般註冊
			case 1:
				
				if(!Regex::isNumber($registType, 1))	{$result["status"] = -5; Util::toEchoJson($result); exit;}
				if(!Regex::isEmail($account, 300))		{$result["status"] = -5; Util::toEchoJson($result); exit;}
				if(!Regex::isPassword($password, 4, 50)){$result["status"] = -5; Util::toEchoJson($result); exit;}	
				if(Regex::isHtmlTag($pushNo))			{$result["status"] = -5; Util::toEchoJson($result); exit;}
				if(!Regex::isAccount($deviceNo, 0, 50))	{$result["status"] = -5; Util::toEchoJson($result); exit;}
				if($dao->getAccount($account, 1))		{$result["status"] = -7; Util::toEchoJson($result); exit;}
				
				$token = Util::getDeviceKey(40);
					
				$obj = array(
						"account" 		=> $account,
						"password" 		=> $password,
						"ps_e" 			=> Util::encrypt($password),
						"regist_type" 	=> $registType,
						"push_no" 		=> $pushNo,
						"date_create"	=> Util::getNowDate("Y-m-d H:i:s"),
						"m_type"		=> $phoneType,
						"device_no"		=> $deviceNo,
						"device_key"	=> $token,
						"push_status"	=> $pushStatus
				);
				
				$ret = $dao->insertResigt($obj);
				
				if($ret){
					$mailData = array("email" => $account, "password" => $password, "token" => $token);
					$sendMail = $this->toMail($mailData);
				}else{
					$result = array("status" => -3, "msg" => "", "mId" => 0, "token" => "" );
					Util::toEchoJson($result);
					exit;
				}
				
				if($sendMail == 1){
					
					if($ret){
						$result = array("status" => 1, "msg" => $sendMail, "mId" => $ret, "token" => $token );
						Util::toEchoJson($result);
						
						if($phoneType == 1){
							$push->android("驗證 EMAIL 已寄出，請到 EMAIL 完成驗證手續!", array($pushNo));
						}else if($phoneType == 2){
							$push->ios("驗證 EMAIL 已寄出，請到 EMAIL 完成驗證手續!", array($pushNo));
						}
						
						exit;
					}else{
						$result = array("status" => -3, "msg" => "", "mId" => 0, "token" => "" );
						Util::toEchoJson($result);
						exit;
					}
					
				}else{
					$result["status"] = -10;
					Util::toEchoJson($result);
					exit;
				}
				
				
			//FB 註冊
			case 2 :
				
				$member = $dao->getAccount($account, 2);
				
				if($member){
					
					$result = array(
							"status" => 1,
							"msg" => "",
							"mId" => $member->id,
							"token" => $member->device_key,
							"registType " => $member->regist_type,
							"isPush" => $member->push_status,
							"realname" => $member->realname,
							"nickname" => $member->nickname,
							"gender" => $member->gender,
							"liveZone" => $member->live_zone,
							"county" => $member->county,
							"phone" => $member->phone,
							"noLine" => $member->no_line,
							"noFB" => $member->no_fb,
							"noWechat" => $member->no_wechat,
							"noSkype" => $member->no_skype,
							"noGplus" => $member->no_gplus,
							"showLine" => $member->show_line,
							"showFB" => $member->show_fb,
							"showWechat" => $member->show_wechat,
							"showSkype" => $member->show_skype,
							"showGplus" => $member->show_gplus);
					
					$result["status"] = 1;
					Util::toEchoJson($result);
					exit;
					 
				}else{
					
					$token = Util::getDeviceKey(40);
						
					$obj = array(
							"ps_e" 			=> Util::encrypt(""),
							"account" 		=> $account,
							"regist_type" 	=> $registType,
							"push_no" 		=> $pushNo,
							"date_create"	=> Util::getNowDate("Y-m-d H:i:s"),
							"m_type"		=> $phoneType,
							"device_no"		=> $deviceNo,
							"device_key"	=> $token,
							"push_status"	=> $pushStatus,
							"email_ver"		=> 1
					);
					
					$ret = $dao->insertResigt($obj);
						
					if($ret){
						
						$member = $dao->getAccount($account, $registType);
						
						$result = array(	
								"ps_e" 			=> Util::encrypt(""),
								"status" 		=> 1, 
								"msg" 			=> "", 
								"mId" 			=> $member->id, 
								"token" 		=> $member->device_key,
								"registType " 	=> $member->regist_type,
								"isPush" 		=> $member->push_status,
								"realname" 		=> $member->realname,
								"nickname" 		=> $member->nickname,
								"gender" 		=> $member->gender,
								"liveZone" 		=> $member->live_zone,
								"county" 		=> $member->county,
								"phone" 		=> $member->phone,
								"noLine" 		=> $member->no_line,
								"noFB" 			=> $member->no_fb,
								"noWechat" 		=> $member->no_wechat,
								"noSkype" 		=> $member->no_skype,
								"noGplus" 		=> $member->no_gplus,
								"showLine" 		=> $member->show_line,
								"showFB" 		=> $member->show_fb,
								"showWechat" 	=> $member->show_wechat,
								"showSkype" 	=> $member->show_skype,
								"showGplus" 	=> $member->show_gplus
						);
						
						Util::toEchoJson($result);
						
						if($member->m_type == 1){
							$push->android("Facebook 註冊完成，感謝您支持寵物生命線 APP!", array($member->push_no));
						}else if($$member->m_type == 2){
							$push->ios("Facebook 註冊完成，感謝您支持寵物生命線 APP!", array($member->push_no));
						}
						
						exit;
					}else{
						$result = array("status" => -3, "msg" => "", "mId" => 0, "token" => "" );
						Util::toEchoJson($result);
						exit;
					}
					
				}
				
				Util::toEchoJson($result);
				exit;
				
			//G+註冊			
			case 3 : 
				
				$member = $dao->getAccount($account, 3);
				
				if($member){
					
					$result = array(
							"status" => 1,
							"msg" => "",
							"mId" => $member->id,
							"token" => $member->device_key,
							"registType " => $member->regist_type,
							"isPush" => $member->push_status,
							"realname" => $member->realname,
							"nickname" => $member->nickname,
							"gender" => $member->gender,
							"liveZone" => $member->live_zone,
							"county" => $member->county,
							"phone" => $member->phone,
							"noLine" => $member->no_line,
							"noFB" => $member->no_fb,
							"noWechat" => $member->no_wechat,
							"noSkype" => $member->no_skype,
							"noGplus" => $member->no_gplus,
							"showLine" => $member->show_line,
							"showFB" => $member->show_fb,
							"showWechat" => $member->show_wechat,
							"showSkype" => $member->show_skype,
							"showGplus" => $member->show_gplus);
					
					
					$result["status"] = 1;
					Util::toEchoJson($result);
					exit;
					
					 
				}else{
					
					$token = Util::getDeviceKey(40);
						
					$obj = array(
							"account" 		=> $account,
							"regist_type" 	=> $registType,
							"push_no" 		=> $pushNo,
							"date_create"	=> Util::getNowDate("Y-m-d H:i:s"),
							"m_type"		=> $phoneType,
							"device_no"		=> $deviceNo,
							"device_key"	=> $token,
							"push_status"	=> $pushStatus,
							"email_ver"		=> 1
					);
					
					$ret = $dao->insertResigt($obj);
						
					if($ret){
						
						$member = $dao->getAccount($account, $registType);
						$result = array(
								"status" => 1, 
								"msg" => "", 
								"mId" => $member->id, 
								"token" => $member->device_key,
								"registType " => $member->regist_type,
								"isPush" => $member->push_status,
								"realname" => $member->realname,
								"nickname" => $member->nickname,
								"gender" => $member->gender,
								"liveZone" => $member->live_zone,
								"county" => $member->county,
								"phone" => $member->phone,
								"noLine" => $member->no_line,
								"noFB" => $member->no_fb,
								"noWechat" => $member->no_wechat,
								"noSkype" => $member->no_skype,
								"noGplus" => $member->no_gplus,
								"showLine" => $member->show_line,
								"showFB" => $member->show_fb,
								"showWechat" => $member->show_wechat,
								"showSkype" => $member->show_skype,
								"showGplus" => $member->show_gplus
						);
						Util::toEchoJson($result);
						
						if($member->m_type == 1){
							$push->android("G+ 註冊完成，感謝您支持寵物生命線 APP", array($member->push_no));
						}else if($$member->m_type == 2){
							$push->ios("G+ 註冊完成，感謝您支持寵物生命線 APP", array($member->push_no));
						}
						
						exit; 
					}else{
						$result = array("status" => -3, "msg" => "", "mId" => 0, "token" => "" );
						Util::toEchoJson($result);
						exit;
					}
				}
				
			//未知登入模式
			default :
				$result["status"] = -3;
				Util::toEchoJson($result);
				exit;
				
			}
		}else{
			$result["status"] = 0;
			Util::toEchoJson($result);
			exit;
		}
	}
	
	public function login(){
		
		$result = array("status" => 0, "msg" => "", "mId" => 0, "token" => "" );
		
		$phoneType 		= $_POST['phoneType'];
		$phoneVersion 	= $_POST['phoneVersion'];
		$account 		= $_POST['account'];
		$password 		= Util::encrypt($_POST['password'] ? $_POST['password'] : "");
		$registType 	= $_POST['registType'] ;
		
		$dao = new MemberM();
		
		if(!Regex::isNumber($phoneType, 1))		{$result["status"] = -3; Util::toEchoJson($result); exit;}
		if(!Regex::isFloat($phoneVersion, 30))	{$result["status"] = -3; Util::toEchoJson($result); exit;}
		
		if($phoneType == 1 && $phoneVersion <= ANDROID_1 ){
		
			$member = $dao->getMemberLogin($account, $password, $registType);
			
			if($member){
				if($member->email_ver == 1 || $registType == 2 || $registType == 3){
					$result = array(
							"status" => $member->realname == "" ? 2 : 1 , 
							"msg" => "", 
							"mId" => $member->id, 
							"token" => $member->device_key,
							"registType" => $member->regist_type,
							"isPush" => $member->push_status,
							"realname" => $member->realname,
							"nickname" => $member->nickname,
							"gender" => $member->gender,
							"liveZone" => $member->live_zone,
							"county" => $member->county,
							"phone" => $member->phone,
							"noLine" => $member->no_line,
							"noFB" => $member->no_fb,
							"noWechat" => $member->no_wechat,
							"noSkype" => $member->no_skype,
							"noGplus" => $member->no_gplus,
							"showLine" => $member->show_line,
							"showFB" => $member->show_fb,
							"showWechat" => $member->show_wechat,
							"showSkype" => $member->show_skype,
							"showGplus" => $member->show_gplus
					);
					Util::toEchoJson($result);
					exit;
				}else{
					$result["status"] = -9;
					Util::toEchoJson($result);
					exit;
				}
				
			}else{
				$result["status"] = -8;
				Util::toEchoJson($result);
				exit;
			}
			
		}else{
			Util::toEchoJson($result);
			exit;
		}
	}
	
	public function updateOwnerData(){
		
		$result 		= array("status" => 0, "msg" => "");
		
		$phoneType 		= $_POST['phoneType'];
		$phoneVersion 	= $_POST['phoneVersion'];
		$mId 			= $_POST["mId"];
		$token 			= $_POST["token"];
		
		$realname 		= $_POST["realname"] ? $_POST["realname"] : "" ;
		$nickname 		= $_POST["nickname"] ? $_POST["nickname"] : "" ;
		$gender 		= $_POST["gender"] ? $_POST["gender"] : 0 ;
		$county_zip 	= $_POST["county"] ? $_POST["county"] : 0 ;
		$phone 			= $_POST["phone"] ? $_POST["phone"] : "" ;
		$noLine 		= $_POST["noLine"] ? $_POST["noLine"] : "" ;
		$noFB 			= $_POST["noFB"] ? $_POST["noFB"] : "" ;
		$noWechat 		= $_POST["noWechat"] ? $_POST["noWechat"] : "" ;
		$noSkype 		= $_POST["noSkype"] ? $_POST["noSkype"] : "" ;
		$noGplus 		= $_POST["noGplus"] ? $_POST["noGplus"] : "" ;
		$showLine 		= $_POST["showLine"] ? $_POST["showLine"] : 0 ;
		$showFB 		= $_POST["showFB"] ? $_POST["showFB"] : 0 ;
		$showWechat 	= $_POST["showWechat"] ? $_POST["showWechat"] : 0 ;
		$showSkype 		= $_POST["showSkype"] ? $_POST["showSkype"] : 0 ;
		$showGplus 		= $_POST["showGplus"] ? $_POST["showGplus"] : 0 ;
		$liveZone		= 0;
		
		$dao = new MemberM();
		
		if(!Regex::isNumber($phoneType, 1))		{$result["status"] = -3; Util::toEchoJson($result); exit;}
		if(!Regex::isFloat($phoneVersion, 30))	{$result["status"] = -3; Util::toEchoJson($result); exit;}
		if(!$dao->checkToken($mId, $token))		{$result["status"] = -4; Util::toEchoJson($result); exit;}
		
		if($phoneType == 1 && $phoneVersion <= ANDROID_1){
			
			if($realname == "")		{$result["status"] = -6; Util::toEchoJson($result); exit;}
			if($nickname == "")		{$result["status"] = -6; Util::toEchoJson($result); exit;}
			if($county_zip == "")	{$result["status"] = -6; Util::toEchoJson($result); exit;}
			if($gender == 0)		{$result["status"] = -6; Util::toEchoJson($result); exit;}
			
			if(Regex::isPunctuation($realname, 1))	{$result["status"] = -5; Util::toEchoJson($result); exit;}
			if(Regex::isPunctuation($nickname, 1))	{$result["status"] = -5; Util::toEchoJson($result); exit;}
			if(!Regex::isNumber($gender, 1))		{$result["status"] = -5; Util::toEchoJson($result); exit;}
			if(!Regex::isNumber($county_zip, 5))	{$result["status"] = -5; Util::toEchoJson($result); exit;}
			//if(!Regex::isPhone($phone, 20))			{$result["status"] = -5; Util::toEchoJson($result); exit;}
			//if(!Regex::isAccount($noLine, 0, 300))	{$result["status"] = -5; Util::toEchoJson($result); exit;}
			//if(!Regex::isEmail($noFB, 300))			{$result["status"] = -5; Util::toEchoJson($result); exit;}
			//if(Regex::isHtmlTag($noWechat))			{$result["status"] = -5; Util::toEchoJson($result); exit;}
			//if(!Regex::isAccount($noSkype, 0, 300))	{$result["status"] = -5; Util::toEchoJson($result); exit;}
			//if(!Regex::isEmail($noGplus, 300))		{$result["status"] = -5; Util::toEchoJson($result); exit;}
			//if(!Regex::isNumber($showLine, 1))		{$result["status"] = -5; Util::toEchoJson($result); exit;}
			//if(!Regex::isNumber($showFB, 1))		{$result["status"] = -5; Util::toEchoJson($result); exit;}
			//if(!Regex::isNumber($showWechat, 1))	{$result["status"] = -5; Util::toEchoJson($result); exit;}
			//if(!Regex::isNumber($showSkype, 1))		{$result["status"] = -5; Util::toEchoJson($result); exit;}
			//if(!Regex::isNumber($showGplus, 1))		{$result["status"] = -5; Util::toEchoJson($result); exit;}
			
			if($county_zip >= 100 && $county_zip <= 208){
				$liveZone = 1;
			}else if($county_zip >= 209 && $county_zip <= 212){
				$liveZone = 5;
			}else if($county_zip >= 220 && $county_zip <= 369){
				$liveZone = 1;
			}else if($county_zip >= 400 && $county_zip <= 655){
				$liveZone = 2;
			}else if($county_zip >= 700 && $county_zip <= 815){
				$liveZone = 3;
			}else if($county_zip >= 817 && $county_zip <= 819){
				$liveZone = 5;
			}else if($county_zip >= 820 && $county_zip <= 852){
				$liveZone = 3;
			}else if($county_zip >= 880 && $county_zip <= 896){
				$liveZone = 5;
			}else if($county_zip >= 900 && $county_zip <= 947){
				$liveZone = 3;
			}else if($county_zip >= 950 && $county_zip <= 983){
				$liveZone = 4;
			}else{
				$liveZone = 6;
			}
			
			$county = $dao->getCounty($county_zip);
			
			$obj = Array(
					"id" 			=> $mId,
					"realname" 		=> $realname,
					"nickname" 		=> $nickname,
					"gender"		=> $gender,
					"live_zone" 	=> $liveZone,
					"county" 		=> $county,
					"phone" 		=> $phone,
					"no_line" 		=> $noLine,
					"no_fb" 		=> $noFB,
					"no_wechat" 	=> $noWechat,
					"no_skype" 		=> $noSkype,
					"no_gplus" 		=> $noGplus,
					"show_line" 	=> $showLine,
					"show_fb" 		=> $showFB,
					"show_wechat" 	=> $showWechat,
					"show_skype" 	=> $showSkype,
					"show_gplus" 	=> $showGplus
			);
			
			$ret = $dao->update($obj);
			
			if($ret){
				$result = array("status" => 1, "msg" => "" );
				Util::toEchoJson($result);
				exit;
			}else{
				$result = array("status" => -3, "msg" => "" );
				Util::toEchoJson($result);
				exit;
			}
			
		}else{
			Util::toEchoJson($result);
			exit;
		}
	}
	
	private function toMail($data){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_URL, EMAIL_PATH);
		curl_setopt($ch, CURLOPT_POST, true);		
		curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query( $data ) );
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_USERAGENT, "bloodPet");
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		$replyData = curl_exec($ch);
		curl_close($ch);
		return $replyData;
	}
	
}

if (isset($_POST["action"])) {
	$service = new AppOwnerS ();
	$service->__call ( $_POST["action"], null );
} 
?>